package cn.neusoft.neusoft_fz.controller;


import cn.neusoft.neusoft_fz.common.ResponseEntity;
import cn.neusoft.neusoft_fz.service.PaymentService;
import org.example.entity.Payment;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2023-06-25
 */
@RestController
@RequestMapping("/payment")
public class PaymentController {

    @Autowired
    private PaymentService paymentService;

    @PostMapping("/getByDate")
    public ResponseEntity<List<Payment>> getByDatePaymentsBySubstationAndTime(
            @RequestParam("substationId") Long substationId,
            @RequestParam("startTime") String startTimeString,
            @RequestParam("endTime") String endTimeString
    ) {
        // 请注意，你可能需要添加时间格式的验证，并处理可能的解析异常
//        LocalDateTime startTime = LocalDateTime.parse(startTimeString);
        Instant instants = Instant.parse(startTimeString);
        LocalDateTime ldts = LocalDateTime.ofInstant(instants, ZoneId.systemDefault());

//        LocalDateTime endTime = LocalDateTime.parse(endTimeString);
        Instant instante = Instant.parse(endTimeString);
        LocalDateTime ldte = LocalDateTime.ofInstant(instante, ZoneId.systemDefault());


        List<Payment> paymentsBySubstationAndTime = paymentService.getPaymentsBySubstationAndTime(substationId, ldts,ldte);
        return ResponseEntity.success(paymentsBySubstationAndTime);
    }

    @PostMapping("/get")
    public ResponseEntity<List<Payment>> getPaymentsBySubstationAndTime(
            @RequestParam("substationId") Long substationId
    ) {


        List<Payment> paymentsBySubstationAndTime = paymentService.getPaymentsBySubstation(substationId);
        return ResponseEntity.success(paymentsBySubstationAndTime);
    }
}
